// pages/team/teamLeader/teamLeader.js
var app = getApp();
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      is_seek:0,
      relatedGoods: [],
      popupView:false,
      speraderInfo:[],
      emptyAvatar:'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png',
      number:0,
  },
  hideCover(){
    this.setData({
      popupView: false
    })
  },
  // 获取推荐商品
  getGoodsRelated: function() {
    let that = this;
    util.request(api.GoodsRelated, {
      id: 1181287,
    }).then(function(res) {
      // console.log(111111);return false;
      if (res.errno === 0) {
        that.setData({
          relatedGoods: res.data.goodsList,
        });
      }
    });
  },
  changePopupView(){
    this.setData({popupView:!this.data.popupView})
},
bindKeyInput(e){
  this.setData({
    number:e.detail.value,
  });
},
ok(){
  let that = this;
  var regex = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  if (!regex.test(that.data.number)) {
    // 如果不是数字，可以给出提示或者执行其他操作
    wx.showToast({
      title: '请输入有效的金额',
      icon: 'none'
    });
  } 
  if(that.data.number == 0){
    wx.showToast({
      title: '请输入金额',
      icon: 'none'
    });
     return false;
  }
  // if(that.data.number < 20){
  //   wx.showToast({
  //     title: '最低提现金额20',
  //     icon:"none",
  //   })
  //   return false;
  // }
  // console.log("请对接接口");
    //请求后端获取解密后的手机号
    util.request(api.spreaderMoney, {
      userId: wx.getStorageSync("userId"),
      money:that.data.number,
    }).then(function(res) {
      if (res.errno == 0) {
          wx.showToast({
            title: '提交成功',
          });
          that.setData({popupView:false})
      }else{
        util.showErrorToast(res.errmsg);
      }
    });
},
withdrawal(){
  this.setData({popupView:true})
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
      if (!app.globalData.hasLogin) {
        wx.navigateTo({
          url: "/pages/auth/login/login"
        });
        return false;
      }
      //登录之后获取接口数据
      this.getIsSpeader();
      this.getGoodsRelated();
  },
  // 获取首页数据信息
  spreaderIndexInfo: function() {
    let that = this;
    let userId = wx.getStorageSync("userId")
    util.request(api.spreaderIndex, {
      userId: userId,
    }).then(function(res) {
      console.log(res)
      if (res.errno === 0) {
        that.setData({
          speraderInfo: res.data,
        });
      }else{
        wx.showToast({
          title: res.errmsg
        })
      }
    });
  },
  getIsSpeader: function() {
    let that = this;
    let userId = wx.getStorageSync("userId")
    util.request(api.getIsSpeader, {
      userId: userId,
    }).then(function(res) {
      // console.log(res);
      if (res.errno === 0) {
        if(res.data == 1){
          //请求团长数据
          that.spreaderIndexInfo();
          return false;
        }
          wx.redirectTo({
            url: '/pages/team/noteamLeader/noteamLeader'
          });
      }else{
        wx.showToast({
          title: res.errmsg
        })
      }
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
      return false;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    // console.log(888888);return false;
    let that = this;
    return {
      title: "欢迎来到易洋优品",
      desc: 'e洋优品，值得你购买',
      path: '/pages/index/index?user_id=' + (wx.getStorageSync("userId")),
      imageUrl: "https://yiyangyoupin.com/img/rjrw4r4e49puifaulr5u.png", //内容图片
    }
  }
})